clear all
global system "linux"

if "${system}" == "linux" {
	global code "/"
	global s "/"
}

run "${code}${s}_set-path.do"
cd "${code}/tba_elig"

use "${temp}/embs-lps-matched.dta", clear  // 12c-embs-lps-match.do


// specifications
do "_embs-lps-spec.do"

gen hibal = orig_loan_amount > cll

// prepayments
** Merge performance variables
qui su termination_date
gen max_age = mofd(`r(max)') - orig_ym + 1
gen last_age = mofd(termination_date) - orig_ym + 1
replace last_age = . if inlist(termination_type_id, "6", "M")==1


foreach Age in 36 60 {
	gen pp`Age' = 0 if max_age >= `Age'
	replace pp`Age' = 1 if pp`Age'==0 & last_age <= `Age'
}

foreach x in appraisal_amount orig_loan_amount {
	replace `x' = `x'/1000
}

eststo clear
eststo: qui estpost su HiP hibal notba pp36 appraisal_amount orig_loan_amount orig_rate credit_score , d

label var HiP "Share of loans with appraisal value > 1.25xCLL"
label var hibal "Share of high-balance loans"
label var notba "Share of loans in TBA-ineligible MBS"
label var appraisal_amount "Appraisal Value (\textdollar 1,000)"
label var orig_loan_amount "Loan Size (\textdollar 1,000)"
label var orig_rate "Mortgage rate (\%)"
label var credit_score "Credit score"
label var pp36 "Share of loans paid off by loan age 36 months"

global keepvar "keep(appraisal_amount orig_loan_amount orig_rate credit_score)"

esttab using "${table}/embs-lps-sumstat.tex", label replace nomtitle nonumber /// 
  stats(N, fmt(%9.0fc) label("N. Obs."))  ///
 cell("mean(fmt(%11.2fc)) min($keepvar) p25($keepvar) p50($keepvar) p75($keepvar) max($keepvar)") collabels("Mean" "Min" "p25" "Median" "p75" "Max")
